#include "DriverEngagementLevelAnalysis.h"
#include <time.h>
#include <string.h>
//#include <sys/time.h>
//#include "mlog.h"
#define EyesStatus_MAX_NUM		10		//看路状态缓冲最大数量
int shortDistract[SHORT_DISTRACT_SIZE] = {0};
Dis_Status_t dis_state;
static bool b_shortdistract_interactive_state = false; 
static int distract_stateback = E_NOTACTIVE_STATE;
static int unresponsive_stateback = D_NOTACTIVE_STATE;
static EyesStatus_t EyesStatus[EyesStatus_MAX_NUM];
uint32_t EyesStatusCouter=0;
uint32_t EyesState=0;
extern int GetSysTime(void);//系统运行时间，单位: ms
	
/* 功能：CAN发送DMS数据前调用该函数，得到最新的分心数据后更新到DMS数据中再发送。 
	形参：out_DrvrDistr: 分心结果
	返回：true or false
*/
bool DriverEngagementLevelAnalysis(DrvrDistr_t out_DrvrDistr)
{
	/* 在此添加分心等级逻辑分析代码，并给出分心结果到out_DrvrDistr中 */
	out_DrvrDistr.DMS_1_DrvrDistrLvl = distract_stateback;//分心结果
	out_DrvrDistr.DMS_1_DrvUnrspvSt = unresponsive_stateback;//未响应结果
	return true;
}

/* 功能：缓存眼睛状态。接收到B0指令后调用该函数。 */
void DMS_CacheEyesStatus(DMS_DrvrAlertSt in_AlertSt)
{
	/* 在此添加分心等级逻辑代码，并给出分心结果到DrvrDistrLvl中 */
	EyesState= in_AlertSt;//获取看路状态

	// EyesStatus[EyesStatusCouter].EyesSt = in_AlertSt;
	// EyesStatus[EyesStatusCouter].time = GetSysTime();
// 	if(EyesStatusCouter < EyesStatus_MAX_NUM)
// 	{
// 		EyesStatusCouter++;
// 	}
// 	else
// 	{
// 		EyesStatusCouter=0;
// 	}
}
/*需要一个10ms的定时器，不停的执行distract_state()*/
void distract_state(void)
{
	static int index = 0; 
	switch (EyesState)
	{
	case NotDetermined:
		{
			/* 不确定态，清掉专心，长分心计数*/
			dis_state.attentive_time = 0; 
			dis_state.long_time = 0;
			distract_stateback = E_NOT_DETERMINED_STATE;
		}
		break;
	case EyesOnRoad:
		{
			/* 清掉长分心计时，开始专心计数，到达2S,设置短分心清除状态*/
			dis_state.attentive_time++;
			dis_state.long_time = 0;
			distract_stateback = E_ATTENTIVE_STATE;
			b_shortdistract_interactive_state = false;
			if (dis_state.attentive_time >= ATTENTIVE_TIME)
			{
				b_shortdistract_interactive_state = true;//短分心清除状态为真
			}
			break;
		}
	case EyesOffRoad:
		{
			/*清掉专心，长分心计时*/
			dis_state.attentive_time = 0;
			dis_state.long_time++;
			b_shortdistract_interactive_state = false;
			if (dis_state.long_time  >= LONG_DISTRACT_TIME)
			{
				distract_stateback = E_LONG_DISTRACT_STATE;//连续分心3S，长分心状态
				b_shortdistract_interactive_state = true;
			}
			if (dis_state.long_time  >= UNRESPONSIVE_TIME)
			{
				unresponsive_stateback = D_UNRESPONSIVE_STATE;//连续分心6S，未响应状态
			}
			else
			{
				unresponsive_stateback = D_NOTACTIVE_STATE;//为连续分心6S，未响应状态清除
			}
			break;
		}
	default:
		break;
	}
	if (b_shortdistract_interactive_state)//短分心清除状态为真，清除状态buff
    {
        memset(shortDistract,0x00,sizeof(shortDistract));
    }
    index = (index % SHORT_DISTRACT_SIZE);
    shortDistract[index] = EyesState; //on road=2
    dis_state.short_time = 0;
    for(int i = 0; i < SHORT_DISTRACT_SIZE; i++){
        if (shortDistract[i] == 2) //计算buff内的不看路状态
        {
            dis_state.short_time++;
        }
    } 
	if (dis_state.short_time >= SHORT_DISTRACT_TIME)
	{
		distract_stateback = E_SHORT_DISTRACT_STATE;//30S内分心10S以上，短分心状态
	}
    index++;
	//Myt_Log(INFO,"dis_state.short_time:%d dis_state.attentive_time:%d dis_state.long_time:%d \n",dis_state.short_time,dis_state.attentive_time,dis_state.long_time);
	//Myt_Log(INFO,"distract_stateback:%d unresponsive_stateback:%d \n",distract_stateback,unresponsive_stateback);
}
//unsigned int time_stamp(void)
//{
//    struct timeval lTime;
//    gettimeofday(&lTime, 0);
//    return (lTime.tv_sec * 1000 + lTime.tv_usec/1000);
//}

